package com.example.springboot.obj;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Data
public class ZjuCasOauth2Profile {

    private String id;

    private Attribute attribute;

    private List<JSONObject> attributes;

    @Data
    public static class Attribute{

        @JSONField(name = "CODE")
        private String code;

        @JSONField(name = "DWH")
        private String dwh;

        @JSONField(name = "XM")
        private String xm;

        @JSONField(name = "ZJHM")
        private String zjhm;

        @JSONField(name = "YHLX")
        private String yhlx;

        @JSONField(name = "DWMC")
        private String dwmc;
    }

    public Attribute getAttribute() {
        if (Objects.isNull(attribute)){
            List<JSONObject> attributesObj = this.getAttributes();
            if (CollectionUtils.isEmpty(attributes)){
                return attribute;
            }
            attribute = new Attribute();
            for (JSONObject obj:attributesObj){
                if (obj.containsKey("CODE")){
                    attribute.setCode(obj.getString("CODE"));
                }
                if (obj.containsKey("YHLX")){
                    attribute.setYhlx(obj.getString("YHLX"));
                }
                if (obj.containsKey("DWH")){
                    attribute.setDwh(obj.getString("DWH"));
                }
                if (obj.containsKey("XM")){
                    attribute.setXm(obj.getString("XM"));
                }
                if (obj.containsKey("DWMC")){
                    attribute.setDwmc(obj.getString("DWMC"));
                }
                if (obj.containsKey("ZJHM")){
                    attribute.setZjhm(obj.getString("ZJHM"));
                }
            }
        }
        return attribute;
    }
}
